#include "ulti.h"
#include "dpalgo.h"
#include "dataset.h"
#include "bruteforce.h"
#include "algo.h"

#define SHELL

using namespace std;

static int test_nums = 0;
static int right_nums = 0;

void RunAndComp(DataSet &ds) {
	FILE *file;
	double duration;
	clock_t start, end;
	int cnt, res, times;

	file = fopen("comp.log", "a+");
	fprintf(file, "%d\t%d\t%d\t\t%d\t\t", ds.flag, ds.id, ds.n, ds.u);

	start = end = duration = 0;

	start = clock();
	BruteForce(ds);
	end = clock();
	if (start - end > 50)
		times = 1;
	else
		times = 5;

	for (cnt = 0; cnt < times; ++cnt){
		start = clock();
		res = DPSubSetSum(ds);
		end = clock();
		duration += end - start;
	}
	duration /= times;
	fprintf(file, "%.3F\t\t%d\t\t", duration, res);


	start = end = duration = 0;
	for (cnt = 0; cnt < times; ++cnt){
		start = clock();
		res = BruteForce(ds);
		end = clock();
		duration += end - start;
	}
	duration /= times;
	fprintf(file, "%.3F\t\t%d\t\t", duration, res);

	start = end = duration = 0;
	for (cnt = 0; cnt < times; ++cnt){
		start = clock();
		res = MaxSubSetSum(ds);
		end = clock();
		duration += end - start;
	}
	duration /= times;
	fprintf(file, "%.3F\t\t%d\n", duration, res);

}

int main(int argc, char **args) {
#ifdef SHELL
	char c;
	bool flag;
	DataSet ds;
	int id, ans, std_ans;
	vector<int> res;

	while (true) {
		printf("Choose the data set to be loaded\n");
		printf("Input 1-7 for data set from file, x for random one, and q for quit\n$ ");
		flag = true;
		cin >> c;
		switch(c) {
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
				id = c - '1';
				break;

			case 'x':
				flag = false;
				break;
			default:
				printf("Wrong command!\n");
			case 'q':
				return 0;
		}
		++test_nums;
		if (flag)
			GetDataSet(ds, id);
		else
			GetRandomDataSet(ds);

		res = all_subsetsum(ds.nums, ds.u);

		DispDataSet(ds);
		ans = res[res.size() - 1];
		std_ans = GetStdSolution(ds);

		printf("Ans: %d, Std Ans %d\n", ans, std_ans);
		if (ans == std_ans) {
			printf("Right\n");
			++right_nums;
		} else {
			printf("False\n");
		}

		printf("Test_nums: %d, rate of right times %.2f%\n\n", 
				test_nums, right_nums * 1.0 / test_nums * 100);
		RunAndComp(ds);
	}
#endif

	return 0;
}